package com.theeviljames.syntax;

import java.util.Vector;

public class ASTNode {
	
	private String literal;
	private byte type;
	public static final byte TERMINAL = 0, NONTERMINAL = 1;
	private byte tokenType;
	private ASTNode parent;
	/*private ASTNode left;
	private ASTNode center;
	private ASTNode right;
	*/
	private Vector<ASTNode> nodes;
	
	public ASTNode(String literal, byte type, byte tokenType) {
		nodes = new Vector<ASTNode>();
		this.literal = literal;
		this.type = type;
		this.tokenType = tokenType;
	}

	
	public ASTNode getParent() {
		return parent;
	}

	public void setParent(ASTNode parent) {
		this.parent = parent;
	}

	public void addASTNode(ASTNode node){
		node.setParent(this);
		nodes.add(0,node);
	}
	
	public Vector<ASTNode> getNodes(){
		return nodes;
	}
	
	public String getLiteral() {
		return literal;
	}

	public void setLiteral(String literal) {
		this.literal = literal;
	}

	public byte getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}
	
	public byte getTokenType() {
		return tokenType;
	}


	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "AST Node> Literal> " + literal + ", Type> " + type + ", TokenType> " + tokenType;
	}
	
	public int depth(){
		ASTNode p = parent;
		int depth = 0;
		while(p!=null){
			p = p.getParent();
			depth++;
		}
		return depth;
	}
	public void print(){
		System.out.println(toString());
		for(ASTNode a:nodes){
			String s = "\t";
			for(int i = 0; i < depth(); i++)s +="\t";
			System.out.print(s);
			a.print();	
		}
	}
	
	@Override
	protected Object clone() throws CloneNotSupportedException {
		ASTNode clone = new ASTNode(literal,type,tokenType);
		for(ASTNode a:nodes)clone.addASTNode(a);
		return clone;
	}
}
